package com.iteaj.iot.test.message.line;

import com.alibaba.fastjson.JSONObject;
import com.iteaj.iot.ProtocolType;
import com.iteaj.iot.test.TestProtocolType;
import com.iteaj.iot.test.message.TMessageHead;
import com.iteaj.iot.utils.ByteUtil;

public class LineMessageHead extends TMessageHead {

    protected LineMessageHead() {
        super(null, null, null);
    }

    protected LineMessageHead(byte[] message) {
        super(message);
    }

    protected LineMessageHead(String equipCode, String messageId, ProtocolType type) {
        super(equipCode, messageId, type);
    }

    public static LineMessageHead build(byte[] message) {
        JSONObject jsonObject = JSONObject.parseObject(new String(message));
        return new LineMessageHead(jsonObject.getString("equipCode"),
                jsonObject.getString("messageId"), jsonObject.getObject("type", TestProtocolType.class));
    }

    public static LineMessageHead build(String equipCode, String messageId, ProtocolType type) {
        LineMessageHead lineMessageHead = new LineMessageHead(equipCode, messageId, type);
        byte[] bytes = JSONObject.toJSONBytes(lineMessageHead);

        lineMessageHead.setMessage(bytes);
        return lineMessageHead;
    }

    @Override
    public TestProtocolType getType() {
        return super.getType();
    }
}
